Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
The outputlist argument can contain the following parts, all of which are optional: • expression—Any numeric or string expression. • Spc(n)—Inserts n space characters in the output. • Tab(n)—Positions the insertion point at absolute column number n. If n is omitted, Tab positions the insertion point at the beginning of the next print zone. If outputlist has more than one item, the items can be separated either with a space or a semicolon. After the Print method is executed, the default ensures that the output of the next Print method is placed on the next line. You can modify the default by placing one of the following arguments at the end of outputlist : • ; (semicolon)—Subsequent output follows immediately on the same line. • Tab(n)—Subsequent output follows at the specified column number on the same line. • Tab()—Subsequent output follows at the next print zone on the same line. Most of the fonts you will use in Visual Basic have proportionally spaced characters in which a wide letter (such as a W) occupies more horizontal space than a narrow letter (such as an i). Therefore, no correlation exists between the number of characters printed and the amount of horizontal space used. You can avoid this problem by using a fixed-pitch font, such as Courier, in which all characters occupy the same amount of horizontal space. Proportional fonts are nicer looking, however, so you’ll usually want to use them. When using proportional fonts, how do you know when the output of the Print method is going to reach the right edge of the object being printed on, making it necessary to start a new line? Remember that the Print method outputs text at the object’s current position. If we know the width of the next chunk of text to be printed, we can do the following (as expressed in pseudocode):
    If ((current X position) + (Width of text)) > (width of object) then
        it won't fit; advance to beginning of next line
    else
        it will fit; output on current line
    end if
You can obtain the width of a unit of text using the TextWidth method. All objects to which the Print method applies support this method. If you pass a string as the argument, TextWidth returns the width that the text would occupy if printed or displayed, expressed in the object’s current horizontal coordinate units. The value takes into account the object’s current Font property setting. How do you advance a line? The best way is to manipulate the object’s CurrentY property directly. First, obtain the height of a line of text using the TextHeight method. TextHeight works just as TextWidth, returning the height (in object units) of the specified text in the current font. The value returned by TextHeight includes the leading, or space between lines of text. Assume that the variable S$ contains the next unit of text to be printed on Form1. Assume also that the variables LMargin and RMargin contain the desired left and right margins, respectively. The following code will wrap to the next line if needed:
    If ((Form1.CurrentX + Form1.TextWidth(S$)) > (Form1.ScaleWidth - RMargin)
    Then
        Form1.CurrentX = LMargin
        Form1.CurrentY = Form1.CurrentY + Form1.TextHeight(S$)
    End If

    Form1.Print S$;
### The Line And Shape Controls If you need to draw permanent lines and shapes on a form, the Line and Shape controls are your best bet. By permanent, I mean that you do not need to modify the drawn objects during program execution. What you draw during program design is exactly what will be displayed during program execution. For this reason, they are most often used for decorative purposes, such as providing interesting borders for controls or dividing a form into sections. Neither of these controls has any associated events. Figure 12.13 shows a Picture Box with a border created with several Shape controls, each slightly larger than the next. To use the Line or Shape control, simply place it on your form as you would any other control. You can choose from the following properties to change the Line control’s appearance: Figure 12.13 Using the Shape control to create borders. • BorderColor—The line color. • BorderWidth—The width of the line, in arbitrary units from 1 to 8,192. • BorderStyle—The style of the line (solid, dotted, dashed, etc.). • DrawMode—The appearance of the line. The default setting gives a line in the color specified by the BorderColor property. Other settings are explained in Visual Basic Help. The Shape control has the previous four properties, plus some additional ones: • Shape—The type of shape. Choices are rectangle, square, rounded rectangle, rounded square, circle, and oval. • BackStyle—Indicates whether the interior of the shape is transparent or opaque. • BackColor—The color of the shape’s interior. This is ignored if BackStyle is set to its default value, Transparent. • FillColor and FillStyle—These properties do the same job as BackColor and BackStyle. If both BackStyle and FillStyle are set to Opaque, the setting of FillColor takes precedence over the setting of BackColor. You can, therefore, switch a shape’s interior color between two values by toggling FillStyle between Transparent and Opaque. TIP: Exploring Visual Basic Help Don’t forget to make regular visits to Visual Basic’s online Help system. It’s loaded with all the little details that I can’t include in the book. Browsing the Help system and looking at the sample code that is provided will provide you with a lot of useful information. ### Printing With The Printer Object So far, we have limited ourselves to displaying graphics and text on screen. Often, you’ll need to send graphics and text to the printer as well. Visual Basic makes this easy with the Printer object. The Printer object has the same properties and methods as the Form and Picture Box objects (those properties and methods related to graphics, at least). Therefore, if you want to print text to the printer, write:
    Printer.Print message
The same goes for the Line, Circle, and Pset methods. Just like the Form or Picture Box objects, the Printer object has a current position defined by the CurrentX and CurrentY properties, the ability to report the width and height of text with the TextHeight and TextWidth methods, the ability to act as the destination (but not the source) for the PrintPicture method, and so on. For the most part, you can do the same things with the Printer object or a Form object, depending on whether you want the output displayed on the screen or sent to the printer.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.